<?php


namespace app\home\controller;

use app\home\model\User as muser;
use think\Db;
use think\facade\Config;
use app\common\library\Hash;
use app\common\library\Yunpian;
use think\Validate;
use think\facade\Request;

class User extends Base
{
    protected $beforeActionList = [
        'first',
    ];

    protected function first()
    {
        $this->assign('nav', 'user');
    }

    public function index()
    {
        if (session('uid')) {
            $this->redirect('/home/homeuser/index/index');

        } else {
            $this->redirect('/home/user/login');
        }
    }

    public function forget()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            $validate = Validate::make([
                'mobile|手机号码' => 'require|mobile',
                'repassword|重复密码' => 'require|confirm:password',
                'password|密码' => 'require|length:6,16',
            ]);
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            if (session('mobile_code_time') < time()) {
                $this->error('短信验证码已经超时或没有发送成功');
            }
            if (session('mobile_code') != 'm' . $post['mobile'] . $post['code']) {
                $this->error('短信验证码错误');
            }
            $user = new muser();
            $post['password'] = Hash::hash($post['password']);
            if ($user->where('mobile', $post['mobile'])->update(['password' => $post['password']]) !== false) {
                session('mobile_code_time', null);
                session('mobile_code', null);
                $this->success('恭喜您密码找回成功，现在将转到登录页面');
            }
            $this->error('密码找回失败，请稍后再试');


        } else {
            return view();
        }
    }

    public function forgetcode()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            $validate = Validate::make([
                'mobile|手机号码' => 'require|mobile',
                'captcha|图形码' => 'require|captcha'
            ]);
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            $user = new muser();
            if ($user->where('mobile', $post['mobile'])->find()) {

            } else {
                $this->error('没有找到对应的手机号码');
            }
            if (session('mobile_code_time') > time()) {
                $this->error('上次发送的验证码还有效，请' . (session('mobile_code_time') - time()) . '秒后稍后再试');
            }
            $code = rand(1000, 9999);
            session('mobile_code', 'm' . $post['mobile'] . $code);
            session('mobile_code_time', time() + 120);
            $yunpian = new Yunpian();
            $reinfo = $yunpian->send(
                config('param.yunpian_apikey'),
                config('param.yunpian_tpl_id'),
                $post['mobile'],
                $code
            );
            if ($reinfo == '0') {
                $this->success('验证码已经发送，请查收');
            } else {
                $this->error('发送错误：' . $reinfo);
            }
        } else {
            $this->redirect('/home/user/login');
        }
    }

    public function getcode()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            $validate = Validate::make([
                'mobile|手机号码' => 'require|mobile|unique:user',
                'captcha|图形码' => 'require|captcha'
            ]);
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            if (session('mobile_code_time') > time()) {
                $this->error('上次发送的验证码还有效，请' . (session('mobile_code_time') - time()) . '秒后稍后再试');
            }
            $code = rand(1000, 9999);
            session('mobile_code', 'm' . $post['mobile'] . $code);
            session('mobile_code_time', time() + 120);
            $yunpian = new Yunpian();
            $reinfo = $yunpian->send(
                config('param.yunpian_apikey'),
                config('param.yunpian_tpl_id'),
                $post['mobile'],
                $code
            );
            if ($reinfo == '0') {
                $this->success('验证码已经发送，请查收');
            } else {
                $this->error('发送错误：' . $reinfo);
            }
        } else {
            $this->redirect('/home/user/login');
        }
    }

    public function register()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            $validate = new \app\home\validate\User();
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            if (session('mobile_code_time') < time()) {
                $this->error('短信验证码已经超时或没有发送成功');
            }
            if (session('mobile_code') != 'm' . $post['mobile'] . $post['code']) {
                $this->error('短信验证码错误');
            }
            $user = new muser();
            $post['password'] = Hash::hash($post['password']);
            $post['sys_info'] = DB::name('product')->where('id', $post['product_id'])->value('title');
            if ($user->allowField(true)->save($post) !== false) {
                session('mobile_code_time', null);
                session('mobile_code', null);
                $this->success('恭喜您注册成功，现在将转到登录页面');
            }
            $this->error('注册失败，请稍后再试');
        } else {
            $product = DB::name('product')->select();
            $this->assign('product', $product);
            return view();
        }

    }

    public function login()
    {
        if ($this->request->isPost()) {
            $post = $this->request->only(['mobile', 'password', 'captcha'], 'post');
            $validate = Validate::make([
                'mobile|手机号码' => 'require|mobile',
                'password|密码' => 'require',
                'captcha|验证码' => 'require|captcha',
            ]);
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            $admin = muser::get(['mobile' => $post['mobile']]);
            if (!$admin) {
                $this->error('手机号码不存在');
            }
            if (!Hash::check($post['password'], $admin['password'])) {
                $this->error('密码错误');
            }
            if (1 != $admin['status']) {
                $this->error('该用户已被禁用，无法登陆');
            }
            //登录成功
            $admin['last_login_ip'] = Request::ip();
            $admin['last_login_time'] = time();
            if ($admin->save()) {
                session('username', $admin['username']);
                session('uid', $admin['id']);
                $this->success('登录成功,将进入会员中心');
            }
            $this->error('登录失败');

        } else {
            return view();
        }

    }

}